#define SENSOR_PC

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "CoordCom.h"

#define TIME_BEFORE_SLEEP_MODE  20 //20 seconds for test


int main()
{
    int iIndex;
    FrameAPI myApiFrame;
    initCoord();
    displayRouteTable();
    while(1)
    {
        iIndex = -1;
        printf("<?>DEBUG - Devices waking up\n");
        incrementAllErrorCount();
        setWatchDog(TIME_BEFORE_SLEEP_MODE);
        while(checkWatchDog() == FAUX)
        {
            printf("<?>DEBUG - Waiting for packet\n");
            if(recept(&myApiFrame, 5) != 0)
            {
                if(myApiFrame.content.frame.frameType == API_CMD_RECV_PACKET)
                {
                    handleDataPacket(myApiFrame);
                }
            }
        }
        displayRouteTable();
        while((iIndex = requestDataFromFirstNonResponding()) != -1)
        {
            if(recept(&myApiFrame, 5) != 0)
            {
                if(myApiFrame.content.frame.frameType == API_CMD_RECV_PACKET)
                {
                    handleDataPacket(myApiFrame);
                }
            }
            else
            {
                incrementErrorCount(iIndex);
            }
        }
        sendRequestSleepModeToAllDevice(10);
        //setWatchDog(gl_SleepTimeLeft);
        setWatchDog(10);
        printf("<?>DEBUG - Devices in Sleep Mode\n");
        while(checkWatchDog() == FAUX);
    }

    fermeCom();
    return EXIT_SUCCESS;
}
